component output="false" extends="AbsService" accessors="true" {

	// Factories
	property name="LoginFactory" type="it.millemultimedia.easyBlog.factory.LoginFactory";
	// Delegate
	property name="UserDelegate" type="it.millemultimedia.easyBlog.delegate.UserDelegate";
	// Facade
	property name="SessionMgr" type="it.millemultimedia.util.SessionMgr";
	// Util
	property name="Security" type="it.millemultimedia.util.Security";

	public it.millemultimedia.easyBlog.service.SecurityService function init(){
		
		return this;
	}
	
	/* 
	ritorna i parametri di accesso da username o e-mail
	decripta username e password
	e li setta in un oggetto User che viene passato per riferimento	
	 */
	public boolean function getAccount( required String email ){
		var userList = [];
		var userObj = getUserDelegate().createInstance();
		
		userObj.setEmail( arguments.email );
		
		userList = getUserDelegate().list( userObj );
		
		if( arrayLen( userList ) eq 1 ){
		
			userObj.setUsername( getSecurity().decryptString( userList[1].getUsername() ) );
			userObj.setPassword( getSecurity().decryptString( userList[1].getPassword() ) );
			
			arguments.User = userObj;
			
			return true;
			
		}
		
		return false;
	}
	
	/* 
	esegue il login
	se il login ha successo
	cambia lo stato dell'utente
	altrimenti ritorna un errore
	*/
	public boolean function confirm( required String Uuid ){
		
		var userObj = getUserDelegate().createInstance();
		var loginObj = "";
		var loginResult = [];
		
		userObj.setStatus( 0 );
		userObj.setUuid( arguments.Uuid );
		
		loginResult = getUserDelegate().list( User, false );
		
		if( arrayLen( loginResult ) eq 1 ){
			
			loginObj = getLoginFactory().createInstance( loginResult[1] );
			
			loginObj.setStatus( true );
			
			userObj = loginObj.getUser();
			// cambia lo stato dell' utente
			userobj.setStatus( 1 );
			// salva utente
			getUserDelegate().update( userObj );
		
			getSessionMgr().setValue( 'login', loginObj );
			
			return true;
			
		}
		
		return false;
		
	}
	
	/*
	esegue il login 
	ricava i permessi
	ritorna un oggeto Login composta da User e Permission
	setta in sessione l'oggetto Login
	*/
	public boolean function login( required String username, required String password ){
		
		var userObj = getUserDelegate().createInstance();
		var loginObj = "";
		var loginResult = [];
		var fileMgrConfig = new fileManager.metadata.Config( absolutePath="#expandPath( '#getDirectoryFromPath(cgi.SCRIPT_NAME)#assets/' )#", basePath="files");
		
		// cripta username e password
		userObj.setUsername( getSecurity().encryptString( arguments.username ) );
		userObj.setPassword( getSecurity().encryptString( arguments.password ) );
		
		loginResult = getUserDelegate().list( userObj, false );
		
		if( arrayLen( loginResult ) eq 1 ){
			
			loginObj = getLoginFactory().createInstance( loginResult[1] );
			
			loginObj.setStatus( true );
			
			userObj = loginObj.getUser();
			userObj.setUsername( arguments.username );
			userObj.setPassword( arguments.password );
		
			getSessionMgr().setValue( 'login', loginObj );
			getSessionMgr().setValue( 'config', fileMgrConfig );
			
			
			return true;
			
		}
		
		return false;
	}
	
	/* 
	esegue un login con le credenziali guest
	setta in sessione i permessi
	*/
	public void function initUser(){
		var userObj = getUserDelegate().createInstance();
		var loginObj = "";
		var loginResult = [];
		
		userObj.setUsername( 'guest' );
		userObj.setPassword( 'guest' );
		
		loginResult = getUserDelegate().list( userObj, false );
		
		if( arrayLen( loginResult ) eq 1 ){
			
			loginObj = getLoginFactory().createInstance( loginResult[1] );
			
			getSessionMgr().setValue( 'login', loginObj );
			
		}else{
		
			throw( message="Init user login failed" );
		
		}
		
	}
	
	/* 
	ritorna un oggetto login
	se il login non esiste in sessione richiama la funzione di 
	initUser per settare i valori di default
	*/
	public it.millemultimedia.easyBlog.model.Login function getLogin(){
		
		if( getSessionMgr().exists( 'login' ) eq false ){

			this.initUser();	

		}
	
		return getSessionMgr().getValue( 'login' ); 
	}
	
	/* 
	esegue il logout
	setta i permessi del'utente guest chiamando initUser
	*/
	public void function logout(){
		
		this.initUser();
		
	}
	
}
